package com.xhji.web.config.redis;

import com.xhji.web.config.lua.Lua;
import org.springframework.data.redis.core.script.RedisScript;

public class RedisScripts {

    /**
     * 自增的同时设置过期时间
     */
    public static final RedisScript<Long> INCR_EXPIRE = RedisScript.of(Lua
            .function()
            .define("key", "KEYS[1]")
            .define("expTime","tonumber(ARGV[1])")
            .define("delta","tonumber(ARGV[2])")
            .define("inc", "redis.call('INCRBY', key, delta)")
            .condition("inc", "==", "1", () -> " redis.call('EXPIRE', key, expTime)")
            .end("inc"), Long.class);
}
